package wschat.handle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

/**
 * Created by alex on 15-1-24.
 */
public class MyWebSocketHandler extends TextWebSocketHandler{

    public static final Logger LOG = LoggerFactory.getLogger(MyWebSocketHandler.class);
    
    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        
        LOG.info("Protocol: {}" , session.getAcceptedProtocol());
        LOG.info("BinaryMessageSizeLimit: {}" , session.getBinaryMessageSizeLimit());
        LOG.info("Session ID: {}" , session.getId());
        LOG.info("LocalAddress: {}" , session.getLocalAddress());
        LOG.info("LocalAddress: {}" , session.getRemoteAddress());

        LOG.info("Message : {}" ,message);
        
        session.sendMessage(new TextMessage(String.format("Echo Message : %s",message)));
        
        
        //session.sendMessage();
    }

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        LOG.info("websocket connected: {} , {}" , session.getUri() ,session.getRemoteAddress());
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        LOG.info("websocket miss connected: {} , {} , status : {} , {}" , session.getUri() ,session.getRemoteAddress(), status.getCode(),status.getReason());
    }

    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
        LOG.info("websocket miss error: {} , {} , exception : {} " , session.getUri() ,session.getRemoteAddress(), exception.getMessage() );
    }
}
